<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        :selected-keys="selectedKeys"
        mode="horizontal"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img class="logo" src="../assets/logo4.png" />
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in visibleRoutes" :key="item.path"
          >{{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div style="cursor: pointer; margin-right: 100px">
        <a-dropdown @select="handleSelect" v-model:popup-visible="popupVisable">
          <a-avatar :style="{ backgroundColor: '#14a9f8' }" @click="onClick"
            >{{ store.state.user?.loginUser?.userName ?? "未登录" }}
          </a-avatar>
          <template #content>
            <a-doption @click="showUserInfo">
              <icon-user />
              个人信息
            </a-doption>
            <a-doption @click="logout">
              <icon-import />
              退出登录
            </a-doption>
          </template>
        </a-dropdown>
      </div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/CheckAccess";
import { UserControllerService } from "../../generated";
import message from "@arco-design/web-vue/es/message";

const selectedKeys = ref(["/"]);
//路由跳转后，更新选择的菜单项，使其高亮
const popupVisable = ref(false);
//页面跳转
const router = useRouter();
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
const store = useStore();
const loginUser = store.state.user.loginUser;
//测试修改全局状态,三秒中后自动获取登录状态
// setTimeout(() => {
//   store.dispatch("user/getLoginUser", {
//     userName: "管理员小明",
//     userRole: ACCESS_ENUM.ADMIN,
//   });
// }, 3000);

//过滤出要显示的页面表单，computed可以用watch替代
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // 判断当前用户权限是否满足访问页面需要的权限
    // if (!checkAccess(loginUser, item?.meta?.access as string)) {
    //   return false;
    // }
    //使用上面这种方式computed函数感知依赖项的变化，因为loginUser被定义成const，是常量，所以不会重新渲染
    if (
      !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
    ) {
      return false;
    }
    return true;
  });
});
const onClick = () => {
  if (
    store.state.user?.loginUser?.userName &&
    store.state.user?.loginUser?.userName != "未登录"
  ) {
    popupVisable.value = true;
    return;
  } else {
    router.push("/user/login");
  }
};
const handleSelect = (v: string) => {
  console.log(v);
};

const showUserInfo = () => {
  router.push("/userInfo");
};
const logout = async () => {
  const res = await UserControllerService.userLogoutUsingPost();
  if (res.code === 0) {
    router.push("/user/login");
  } else {
    message.error("退出登录失败" + res.message);
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.title-bar {
  margin-top: 15px;
}

.logo {
  width: 130px;
}
</style>
